#include "TypingFrame.h"
#include "WaitForConect.h"
#include "MainScene.h"

TypingFrame::TypingFrame()
{
	Vec2 visablesize = Director::sharedDirector()->getVisibleSize();
	Sprite* AccountFrame = Sprite::create("Typing_Frame.png");
	AccountFrame->SetPosition(visablesize.x / 2, visablesize.y * 0.70f);
	AccountFrame->setTag(0);
	AccountTyping = TextFieldTTF::textFieldWithPlaceHolder("Please input your account:", "Arial", 10);
	AccountTyping->setTag(1);
	AccountTyping->setPosition(AccountFrame->getContentSize().width / 2, AccountFrame->getContentSize().height / 2);
	AccountFrame->addChild(AccountTyping);
	addChild(AccountFrame);
	TypingButtons.push_back(AccountFrame);

	Sprite* PassWordFrame = Sprite::create("Typing_Frame.png");
	PassWordFrame->SetPosition(visablesize.x / 2, visablesize.y * 0.60f);
	PassWordFrame->setTag(1);
	PassWordTyping = TextFieldTTF::textFieldWithPlaceHolder("Please input your password:", "Arial", 10);
	PassWordTyping->setTag(1);
	PassWordTyping->setPosition(PassWordFrame->getContentSize().width / 2, PassWordFrame->getContentSize().height / 2);
	PassWordFrame->addChild(PassWordTyping);
	addChild(PassWordFrame);
	TypingButtons.push_back(PassWordFrame);

	float FirstX = visablesize.x / 6;
	float SingleX = visablesize.x / 3;
	for (int i = 0; i != 3; i++)
	{
		Sprite* TypingButton = Sprite::create("Typing_Button.png");
		TypingButton->SetPosition(FirstX + (i * SingleX), visablesize.y * 0.40f);
		TypingButton->setTag(i + 2);
		char msg[255];
		snprintf(msg, 255, "Typing_Button_Text_%d.png", i);
		Sprite* TypingText = Sprite::create(msg);
		TypingText->setPosition(TypingButton->getContentSize().width / 2, TypingButton->getContentSize().height / 2);
		TypingButton->addChild(TypingText);
		addChild(TypingButton);
		TypingButtons.push_back(TypingButton);
	}
	TouchedID = -1;
	setTouchMode(kCCTouchesOneByOne);
	setTouchEnabled(true);
}

TypingFrame::~TypingFrame()
{
}

bool TypingFrame::onTouchBegan(CCTouch* touch, CCEvent* ev)
{
	if (sNotifer->isVisible())
		return false;
	for (int i = 0; i < TypingButtons.size(); i++)
	{
		Sprite* Temp = TypingButtons.at(i);
		if (Temp->boundingBox().containsPoint(touch->getLocation()))
		{
			TouchedID = Temp->getTag();
			return true;
		}
	}
	return false;
}

void TypingFrame::onTouchEnded(CCTouch* touch, CCEvent* ev)
{
	Sprite* Temp = (Sprite*)getChildByTag(TouchedID);
	if (!Temp)
		return;
	if (Temp->boundingBox().containsPoint(touch->getLocation()))
	{
		switch (Temp->getTag())
		{
			case TAG_ACCOUNT:
			case TAG_PASSWORD:
			{
				if (TextFieldTTF* ttf = (TextFieldTTF*)Temp->getChildByTag(1))
					ttf->attachWithIME();
				break;
			}
			case TAG_CONFIRM:
			{
				std::string acc = "12",pas = "123";
				//if (AccountTyping->getString().empty() || PassWordTyping->getString().empty())
				//	break;
				//acc = AccountTyping->getString();
				//pas = PassWordTyping->getString();
				sNotifer->Show();
				char msg[255];
				snprintf(msg, 255, "%d|%s|%s", CMSG_PLAYER_LOGIN_CHECK_ACCOUNT, acc.c_str(), pas.c_str());
				sPlayer->AddMsgToList(msg);
				//scheduleUpdate();
				break;
			}
			case TAG_CANCEL:
			{
				AccountTyping->setString("");
				PassWordTyping->setString("");
				break;
			}
			case TAG_BACKTOMAIN:
			{
				sMainScene->SwapLayerByTag(getTag(), MAIN_LAYER_TAG);
				return;
			}
		}
		return;
	}
	TouchedID = -1;
}

void TypingFrame::update(float diff)
{

}